//
//  LoginViewController.m
//  Code
//
//  Created by apple on 2017/10/25.
//  Copyright © 2017年 HD. All rights reserved.
//


#import "LoginViewController.h"
#import "ZCViewController.h"
#import "FPWViewController.h"
#import "MainTabBarController.h"


@interface LoginViewController ()
{
    UIImageView *View;
    UIView *bgView;
    UITextField *userTF;
    UITextField *pwdTF;
    
}

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    View = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    
    UIImageView *headIcon =[UIImageView new];
    [View addSubview:headIcon];
    [headIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.centerX.equalTo(View);
        make.top.equalTo(View).offset(100);
        
    }];
    
    headIcon.image = [UIImage imageNamed:@"logo.png"];
    headIcon.userInteractionEnabled = YES;

    //预留添加背景图片
//    View.image = [UIImage imageNamed:@""];
    
    View.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:View];
    [self createButtons];
    [self createTextFields];
   
}


-(void)createTextFields
{
    bgView=[[UIView alloc]initWithFrame:CGRectMake(10, WIN_HEIGHT/2-85, WIN_WIDTH-20, 100)];
    bgView.layer.cornerRadius=3.0;
    bgView.alpha=0.7;
    bgView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bgView];
    userTF=[self createTextFielfFrame:CGRectMake(60, 10, 271, 30) font:[UIFont systemFontOfSize:14] placeholder:@"请输入您手机号码"];
    userTF.keyboardType=UIKeyboardTypeNumberPad;
    userTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    pwdTF=[self createTextFielfFrame:CGRectMake(60, 60, 271, 30) font:[UIFont systemFontOfSize:14]  placeholder:@"密码" ];
    pwdTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    //密文样式
    pwdTF.secureTextEntry=YES;
    //pwd.keyboardType=UIKeyboardTypeNumberPad;
    
//图标
//    UIImageView *userImageView=[self createImageViewFrame:CGRectMake(20, 10, 25, 25) imageName:@"ic_landing_nickname" color:nil];
//    UIImageView *pwdImageView=[self createImageViewFrame:CGRectMake(20, 60, 25, 25) imageName:@"mm_normal" color:nil];
    UIImageView *line1=[self createImageViewFrame:CGRectMake(20, 50, bgView.frame.size.width-40, 1) imageName:nil color:[UIColor colorWithRed:180/255.0f green:180/255.0f blue:180/255.0f alpha:.0]];
    
    UILabel *userLable = [self createLabelFrame:CGRectMake(20, 10, 30, 25) font:[UIFont systemFontOfSize:14] text:@"账号"];
    
    UILabel *pwdLable = [self createLabelFrame:CGRectMake(20, 60, 30, 25) font:[UIFont systemFontOfSize:14] text:@"密码"];
    
    [bgView addSubview:userTF];
    [bgView addSubview:pwdTF];
    [bgView addSubview:userLable];
    [bgView addSubview:pwdLable];
    [bgView addSubview:line1];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)createButtons
{
    UIButton *landBtn=[self createButtonFrame:CGRectMake(10, WIN_HEIGHT/2 +45, WIN_WIDTH -20, 37) backImageName:nil title:@"登录" titleColor:[UIColor whiteColor]  font:[UIFont systemFontOfSize:19] target:self action:@selector(landClick)];
    
    landBtn.backgroundColor = [UIColor cz_colorWithHex:0x5cb3f4];
    landBtn.layer.cornerRadius=5.0f;
    
    UIButton *newUserBtn=[self createButtonFrame:CGRectMake(5, WIN_HEIGHT/2 +15, 70, 30) backImageName:nil title:@"快速注册" titleColor:[UIColor grayColor] font:[UIFont systemFontOfSize:13] target:self action:@selector(registration:)];
    
    UIButton *forgotPwdBtn=[self createButtonFrame:CGRectMake(self.view.frame.size.width-75, WIN_HEIGHT/2 +15, 60, 30) backImageName:nil title:@"找回密码" titleColor:[UIColor grayColor] font:[UIFont systemFontOfSize:13] target:self action:@selector(fogetPwd:)];
 
    [self.view addSubview:landBtn];
    [self.view addSubview:newUserBtn];
    [self.view addSubview:forgotPwdBtn];
    
    
}

//登录
-(void)landClick
{
    [self.view endEditing:YES];
    if (userTF.text.length<=0 || pwdTF.text.length<=0) {
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"账号密码不能为空"];
        return;
    }
    
    MainTabBarController *nav= [[MainTabBarController alloc]init];
    
    [AppDelegate shareDelegate].window.rootViewController=nav;
     [[TKAlertCenter defaultCenter]postAlertWithMessage:@"登录成功"];
    
}



-(void)registration:(UIButton *)button
{
    [self.navigationController pushViewController:[[ZCViewController alloc]init] animated:YES];
}

-(void)fogetPwd:(UIButton *)button
{
    [self.navigationController pushViewController:[[FPWViewController alloc]init] animated:YES];
}


-(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target action:(SEL)action
{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=frame;
    if (imageName)
    {
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    
    if (font)
    {
        btn.titleLabel.font=font;
    }
    
    if (title)
    {
        [btn setTitle:title forState:UIControlStateNormal];
    }
    if (color)
    {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    if (target&&action)
    {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

-(UITextField *)createTextFielfFrame:(CGRect)frame font:(UIFont *)font placeholder:(NSString *)placeholder
{
    UITextField *textField=[[UITextField alloc]initWithFrame:frame];
    
    textField.font=font;
    
    textField.textColor=[UIColor grayColor];
    
    textField.borderStyle=UITextBorderStyleNone;
    
    textField.placeholder=placeholder;
    
    return textField;
}

-(UILabel *)createLabelFrame:(CGRect)frame font:(UIFont *)font text:(NSString *)text
{
    UILabel *Label=[[UILabel alloc]initWithFrame:frame];
    
    Label.font=font;
    
    Label.textColor=[UIColor grayColor];
    
    Label.text = text;
    
    return Label;
}

-(UIImageView *)createImageViewFrame:(CGRect)frame imageName:(NSString *)imageName color:(UIColor *)color
{
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:frame];

    if (imageName)
    {
        imageView.image=[UIImage imageNamed:imageName];
    }
    if (color)
    {
        imageView.backgroundColor=color;
    }

    return imageView;
}


-(void)touchesEnded:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    [userTF resignFirstResponder];
    [pwdTF resignFirstResponder];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [userTF resignFirstResponder];
    [pwdTF resignFirstResponder];
}

@end
